function createMap(sx, sy){
	var map = [];
	for (var i = 0; i < sx; i++){
		var line = [];
		for (var j = 0; j < sy; j++){
			//blank map with borders
			line.push(i == 0 || i == sx - 1 || j == 0 ||  j == sy - 1 ? 1 : 2);	
		}
		map.push(line);
	}
	
	var maxCells = 300;
	var cellsCounter = 0;
	
	var nextCells = function (point){
		
		
		/*var calculateNeibors = function (x, y){
			var sum = 0;
			for (var i = x-1; i < x + 2; i++){
				for (var j = y-1; j < y + 2; j++){
					sum += isInBounds(map, {x:i,y:j})? map[i][j] : 0; 
				}
			}	
			return sum;
		}*/	
		
		var nextPoint = function(x, y){
			//var cn = calculateNeibors(x,y);
			return {
					valid: !cont(getByPoint(map, {x: point.x + x, y: point.y + y}), [0,1]), 
					point: {x: point.x + x, y: point.y + y},
					i: Math.round(Math.random() * 10)
				};
		}
		
		var n = nextPoint(-1,0);
		var s = nextPoint(1,0);
		var w = nextPoint(0,-1);
		var e = nextPoint(0,1);
		
		var avaliables = [];
		if (n.valid) avaliables.push(n); 
		if (s.valid) avaliables.push(s); 
		if (w.valid) avaliables.push(w); 
		if (e.valid) avaliables.push(e);

		if (avaliables.length > 0){
			avaliables.sort(function (a, b){
				return a.i < b.i ? -1 : 1;
			});	
			return avaliables;
		}
		return false;
	};
	
	var gnawCell = function(point){
		if (cellsCounter < maxCells){
			setByPoint(map, point, 0);		
			cellsCounter++;
		
			var points = nextCells(point);
			forEach(points, function(next){
				if (next){							
					gnawCell(next.point);
				}	
			});
		}
	};
	
	gnawCell({x:3, y:3});

	
	return map;	
}
 
function  drawMap(map, canvas){
	forEach(map, function(line, i){
		forEach(line, function(cell, j){
			var color = cell == 1 ? "#444" : 
						cell == 2 ? "#888" : 
						cell == 0 ? "#FFF" : 
									"#000"; 	
			canvas.rect(i * 8,j * 8, 8,8).attr({fill:color, stroke:color}); 
		});
	});
}